/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ecy.quadratic.ui.fragment;

import android.os.Bundle;

import com.ecy.quadratic.R;
import com.ecy.quadratic.beans.support.BookSearchEvent;
import com.ecy.quadratic.reader.base.BaseRVNPFragment;
import com.ecy.quadratic.reader.bean.SearchDetail;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.reader.ui.activity.BookDetailActivity;
import com.ecy.quadratic.reader.ui.easyadapter.SearchAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 二级排行榜
 *
 * @author yuyh.
 * @date 16/9/1.
 */
public class ComicSearchResultFragment extends BaseRVNPFragment<SearchDetail.SearchBooks> {

    public final static String BUNDLE_ID = "_id";
    private String id;

    public static ComicSearchResultFragment newInstance(String id) {
        ComicSearchResultFragment fragment = new ComicSearchResultFragment();
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_ID, id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.common_easy_recyclerview;
    }

    @Override
    public void initData() {
//        id = getArguments().getString(BUNDLE_ID);
    }

    @Override
    public void configViews() {
        initAdapter(SearchAdapter.class, false, false);
    }

    @Override
    public void attachView() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }


    @Override
    public void onItemClick(int position) {
        BookDetailActivity.startActivity(activity, mAdapter.getItem(position)._id);
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void showBooks(BookSearchEvent event) {
        mAdapter.clear();
        mAdapter.addAll(event.searchBooksList);
        mAdapter.notifyDataSetChanged();
    }
}
